package com.hks360.car_treasure.fragment.control;

import android.content.DialogInterface;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.SystemClock;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TabHost;
import android.widget.TextView;

import com.hks360.car_treasure.R;
import com.hks360.car_treasure.activity.ConnectEquipmentActivity;
import com.hks360.car_treasure.activity.MainActivity;
import com.hks360.car_treasure.common.AppConfig;
import com.hks360.car_treasure.common.Method;
import com.hks360.car_treasure.common.PrefKey;
import com.hks360.car_treasure.fragment.BaseFragment;
import com.hks360.car_treasure.model.CarLocation;
import com.hks360.car_treasure.model.CarState;
import com.hks360.car_treasure.model.TboxInfo;
import com.hks360.car_treasure.multimedia.Sound;
import com.hks360.car_treasure.mvp.equipment.presenter.EquipmentPresenter;
import com.hks360.car_treasure.mvp.equipment.presenter.EquipmentPresenterImpl;
import com.hks360.car_treasure.mvp.equipment.view.EquipmentView;
import com.hks360.car_treasure.socket.CommunicationManager;
import com.hks360.car_treasure.socket.Network;
import com.hks360.car_treasure.socket.PhoneBandDevice;
import com.hks360.car_treasure.util.MyUtil;
import com.hks360.car_treasure.widget.SevenClickView;
import com.hks360.library.util.CommonUtil;
import com.hks360.library.util.PrefUtil;
import com.hks360.library.util.UIUtil;
import com.hks360.library.widget.CheckImageView;
import com.hks360.library.widget.CustomDialog;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Timer;

/**
 * 控制Fragment
 */
public class ControlFragment extends BaseFragment {
    private String mTboxId;

    private CheckImageView mEngineCiv;
    private CheckImageView mFindCarCiv;
    private TextView mFireTv;
    private TextView mMisFireTv;
    private CheckImageView mFrontFrostCiv;
    private CheckImageView mRearFrostCiv;
    private CheckImageView mSearHeatCiv;

    private boolean mEngineClick = false;
    private boolean mFindCarClick = false;
    private boolean mFrontFrostClick = false;
    private boolean mRearFrostClick = false;
    private boolean mSearHeatClick = false;


    private TextView mHandBrakeTv;
    private TextView mGearTv;
    private TextView mLightTv;
    private TextView mCenterControlTv;
    private TextView mDoorTv;
    private TextView mSimTv;
    private TextView mPetrol;
    boolean centerControl = false;
    private boolean handBrake = false;
    private boolean gearNull = false;
    private boolean allDoorClose = false;
    private BaseAdapter mAdapter;
    private final static int CARSTATE = 2;
    private final static int DEVICEONOROFF = 3;
    private final static int NOLOGIN = 4;
    private final static int TOAST = 5;
    private final static int FIRE_ON = 6;
    private final static int FIRE_OFF = 7;
    private final static int TIME = 8;
    private final static int FRONT_DEFROSTING_ON = 9;
    private final static int FRONT_DEFROSTING_OFF = 10;
    private final static int REAR_DEFROSTING_ON = 11;
    private final static int REAR_DEFROSTING_OFF = 12;
    private final static int SEAT_HEATING_ON = 13;
    private final static int SEAT_HEATING_OFF = 14;
    private final static int COOL_WIND = 15;
    private final static int MSG_SEND_CMD_TIMEOUT = 16;

    private boolean isExe = false;
    private boolean CommandExe = false;
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1:
                    mFindCarClick = !mFindCarClick;
                    mFindCarCiv.setChecked(mFindCarClick);
                    initState();
                    break;
                case CARSTATE:
                    getCurrentStateSuccess();
                    break;
                case DEVICEONOROFF:
                    try {
                        initTitleState();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    isExe = true;
                    break;
                case NOLOGIN:
                    try {
                        setServer();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    AppConfig.mComManager.loading(Method.getTboxId(), PrefUtil.getString(getContext(), PrefKey.LOGIN_PHONE, ""), PrefUtil.getString(getContext(), PrefKey.LOGIN_PASSWORD, ""));
                    PrefUtil.putString(getContext(), PrefKey.TBOX_ID, Method.getTboxId());
                    break;
                case TOAST:
//                    CommonUtil.showToast(getContext(), "命令未响应，已取消");
                    mDialog.hide();
                    firstTime = 0;
                    CommandExe = false;
                    break;
                case FIRE_ON:
                    mDialog.hide();
                    MyUtil.setDrawableTop(getContext(), mFireTv, mEngineClick ? R.drawable.fire : R.drawable.misfire);
                    MyUtil.setDrawableTop(getContext(), mMisFireTv, mEngineClick ? R.drawable.misfire : R.drawable.fire);
                    mEngineCiv.setChecked(mEngineClick);
                    mSound.play(Sound.START);
                    break;
                case FIRE_OFF:
                    mDialog.hide();
                    mSound.play(Sound.END);
                    mEngineCiv.setChecked(mEngineClick);
                    MyUtil.setDrawableTop(getContext(), mFireTv, mEngineClick ? R.drawable.fire : R.drawable.misfire);
                    MyUtil.setDrawableTop(getContext(), mMisFireTv, mEngineClick ? R.drawable.misfire : R.drawable.fire);
                    break;
                case TIME:
                    secondTime = SystemClock.currentThreadTimeMillis();
                    count++;
                    break;
                case FRONT_DEFROSTING_ON:
                    mDialog.hide();
                    handler.removeMessages(MSG_SEND_CMD_TIMEOUT);
                    mFrontFrostCiv.setChecked(mFrontFrostClick);
                    break;
                case FRONT_DEFROSTING_OFF:
                    mDialog.hide();
                    handler.removeMessages(MSG_SEND_CMD_TIMEOUT);
                    mFrontFrostCiv.setChecked(mFrontFrostClick);
                    break;
                case REAR_DEFROSTING_ON:
                    mDialog.hide();
                    handler.removeMessages(MSG_SEND_CMD_TIMEOUT);
                    mRearFrostCiv.setChecked(mRearFrostClick);
                    break;
                case REAR_DEFROSTING_OFF:
                    mDialog.hide();
                    handler.removeMessages(MSG_SEND_CMD_TIMEOUT);
                    mRearFrostCiv.setChecked(mRearFrostClick);
                    break;
                case SEAT_HEATING_ON:
                    mDialog.hide();
                    handler.removeMessages(MSG_SEND_CMD_TIMEOUT);
                    mSearHeatCiv.setChecked(mSearHeatClick);
                    break;
                case SEAT_HEATING_OFF:
                    mDialog.hide();
                    handler.removeMessages(MSG_SEND_CMD_TIMEOUT);
                    mSearHeatCiv.setChecked(mSearHeatClick);
                    break;
                case COOL_WIND:
                    mDialog.hide();
                    handler.removeMessages(MSG_SEND_CMD_TIMEOUT);
                    mAirScv.setChangeSuccess(true);
                    break;
                case MSG_SEND_CMD_TIMEOUT:
                    if(mDialog.isShowing() && firstTime != 0){
                        mDialog.hide();
                        firstTime = 0;
                        CommandExe = false;
                        CommonUtil.showToast(getContext(), "发送超时，命令已取消");
                    }
                    break;
            }
        }
    };
    private boolean isFirst = true;
    private boolean isGet = true;

    private TextView mTitleLeft;
    private TextView mTitleRight;
    private SevenClickView mAirScv;
    private ListView mDeviceLv;
    private View contentView;
    private PopupWindow popupWindow;
    private TabHost mTabHost;

    private byte[] mCarStatusBytes;
    private byte[] mLastStateBytes;
    private EquipmentPresenter mEquipement;
    private boolean isNodevice = false;
    private Sound mSound;
    private boolean isFirstInit = true;

    private float firstTime = 0;
    private float secondTime;
    private int mCommandId;
    private int count = 0;
    private Timer timer;
    private Network mNet;


    public void initView() {
        mTabHost = ((MainActivity) getActivity()).getTabHost();
        initTitle();
        initDialog();
        setupView();
    }

    private void initTitleState() throws Exception {
        if (isNodevice) {
            if (Method.getCph().isEmpty() || Method.getCph() == null) {
                if (Method.getTboxId().isEmpty()) {
                    initTitleBar("无设备", 15, Color.RED);
                } else {
                    initTitleBar(getString(R.string.title_control_offline, Method.getTboxId().substring(11)), 15, Color.RED);
                }
            } else {
                initTitleBar(getString(R.string.title_control_offline, Method.getCph()), 15, Color.RED);
            }
        } else {
            if (Method.getCph().isEmpty() || Method.getCph() == null) {
                initTitleBar(getString(R.string.title_control_online, Method.getTboxId().substring(11)), 15);
            } else {
                initTitleBar(getString(R.string.title_control_online, Method.getCph()), 15);
            }
        }
    }

    private void initTitle() {
        mTitleLeft = UIUtil.findViewById(mRootView, R.id.common_titlebar_left_tv);
        mTitleLeft.setText("设备列表");
        mTitleLeft.setVisibility(View.VISIBLE);
        mTitleRight = UIUtil.findViewById(mRootView, R.id.common_titlebar_right_tv);
        mTitleRight.setText("连接设备");
        mTitleRight.setVisibility(View.VISIBLE);
        mTitleRight.setOnClickListener(this);
        mTitleLeft.setOnClickListener(this);

    }
    static class ViewHolder{
        TextView cphTv;
        TextView imeiTv;
        TextView mcuTv;
    }

    @Override
    public void setupView() {
        mNet = new Network(getContext());
        PrefUtil.putInt(getContext(), PrefKey.NETSTYLE, mNet.getNetworkState());
        mNet.setOnNetChangedListener(new Network.OnNetChangedListener() {
            @Override
            public void onNetChanged(int networkState) {
                CommandExe = false;
                if (networkState == Network.NETWORN_NONE) {
                    CommonUtil.showToast(getContext(), "无网络连接，请检查网络");
                    return;
                }
                if (mDialog.isShowing()) {
                    mDialog.hide();
                    if (networkState != PrefUtil.getInt(getContext(), PrefKey.NETSTYLE, 0)) {
                        PrefUtil.putInt(getContext(), PrefKey.NETSTYLE, networkState);
                        CommonUtil.showToast(getContext(), "网络状态发生变化，正在重新登录，请稍等");
                        count = 0;
                        try {
                            if (AppConfig.mComManager.getmSocket() != null) {
                                AppConfig.mComManager.getmSocket().close();
                            }
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                        AppConfig.mComManager.unLoading();
                        AppConfig.mComManager = null;
                        while (AppConfig.mComManager == null) {
                            try {
                                if (AppConfig.mComManager == null) {
                                    AppConfig.mComManager = new CommunicationManager(getContext());
                                    setServer();
                                }
                            } catch (Exception e) {

                            }

                        }
                        PrefUtil.putString(getContext(), PrefKey.TBOX_ID, Method.getTboxId());
                        while (AppConfig.mComManager.getmSocket() == null && count <= 10) {
                            count++;
                            AppConfig.mComManager.loading(Method.getTboxId(), PrefUtil.getString(getContext(), PrefKey.LOGIN_PHONE, ""), PrefUtil.getString(getContext(), PrefKey.LOGIN_PASSWORD, ""));
                            try {
                                Thread.sleep(500);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                        AppConfig.mComManager.startHeartBeat();
                        CommonUtil.showToast(getContext(), "已成功连上服务器");
                        initData();
                    }
                    firstTime = 0;
                    count = 0;
                }
            }
        });
        mEquipement = new EquipmentPresenterImpl(getContext(), new EquipmentView() {
            @Override
            public String getMobile() {
                return null;
            }

            @Override
            public String getVin() {
                return null;
            }

            @Override
            public String getTboxId() {
                return Method.getTboxId();
            }

            @Override
            public void bindUserSuccess() {

            }

            @Override
            public void getCurrentStateSuccess(CarState carState) {

            }

            @Override
            public void readCurrentLocationSuccess(CarLocation carLocation) {

            }

            @Override
            public void readCurrentLocationFailed() {

            }

            @Override
            public void showLoading() {

            }

            @Override
            public void hideLoading() {

            }

            @Override
            public void showToastMsg(String msg) {

            }
        });
        mEquipement.getCurrentState(0);
        mEngineCiv = UIUtil.findViewById(mRootView, R.id.control_engine_civ);
        mFindCarCiv = UIUtil.findViewById(mRootView, R.id.control_findcar_civ);
        mFireTv = UIUtil.findViewById(mRootView, R.id.control_fire_tv);
        mMisFireTv = UIUtil.findViewById(mRootView, R.id.control_misfire_tv);
        mFrontFrostCiv = UIUtil.findViewById(mRootView, R.id.control_frontdefrost_civ);
        mRearFrostCiv = UIUtil.findViewById(mRootView, R.id.control_reardefrost_civ);
        mSearHeatCiv = UIUtil.findViewById(mRootView, R.id.control_seatheat_civ);
        mHandBrakeTv = UIUtil.findViewById(mRootView, R.id.handbrake);
        mGearTv = UIUtil.findViewById(mRootView, R.id.gear);
        mLightTv = UIUtil.findViewById(mRootView, R.id.light);
        mCenterControlTv = UIUtil.findViewById(mRootView, R.id.centercontrol);
        mDoorTv = UIUtil.findViewById(mRootView, R.id.door);
        mSimTv = UIUtil.findViewById(mRootView, R.id.signal_tv);
        mPetrol = UIUtil.findViewById(mRootView, R.id.petrol);
        mAirScv = UIUtil.findViewById(mRootView, R.id.myview);
        mAirScv.setCommunicationManager(AppConfig.mComManager);
        contentView = LayoutInflater.from(getContext()).inflate(
                R.layout.control_list, null);
        mDeviceLv = UIUtil.findViewById(contentView, R.id.control_list);
        if (AppConfig.user.getTboxinfo().size() != 0) {
            AppConfig.user.getTboxinfo().get(Method.getI()).setClicked(true);
        }
        mDeviceLv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                popupWindow.dismiss();
                Method.setI(i);
                for (int j = 0; j < AppConfig.user.getTboxinfo().size(); j++) {
                    if (j == i) {
                        AppConfig.user.getTboxinfo().get(j).setClicked(true);
                    } else {
                        AppConfig.user.getTboxinfo().get(j).setClicked(false);
                    }
                }
                mAdapter.notifyDataSetChanged();
                try {
                    setServer();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                initData();
                try {
                    initTitleState();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
        mAdapter = new BaseAdapter() {
            @Override
            public int getCount() {
                return AppConfig.user.getTboxinfo().size();
            }

            @Override
            public Object getItem(int i) {
                return null;
            }

            @Override
            public long getItemId(int i) {
                return 0;
            }

            @Override
            public View getView(int i, View view, ViewGroup viewGroup) {
                ViewHolder holder;
                if(view == null){
                    view = View.inflate(getContext(), R.layout.control_list_item, null);
                    holder = new ViewHolder();
                    holder.cphTv = UIUtil.findViewById(view, R.id.list_item_cph);
                    holder.imeiTv = UIUtil.findViewById(view, R.id.list_item_imei);
                    holder.mcuTv = UIUtil.findViewById(view, R.id.list_item_mcu);
                    view.setTag(holder);
                }else {
                    holder = (ViewHolder) view.getTag();
                }
                TboxInfo tboxInfo = AppConfig.user.getTboxinfo().get(i);
                if (tboxInfo.getCph() == "") {
                    holder.cphTv.setText("车牌号:暂无");
                } else {
                    holder.cphTv.setText("车牌号:" + AppConfig.user.getTboxinfo().get(i).getCph());
                }
                holder.imeiTv.setText("IMEI:" + AppConfig.user.getTboxinfo().get(i).getTboxid());
                holder.mcuTv.setText("MCU:" + AppConfig.user.getTboxinfo().get(i).getSoftversion());
                if (tboxInfo.isClicked()) {
                    holder.cphTv.setTextColor(Color.GREEN);
                    holder.imeiTv.setTextColor(Color.GREEN);
                    holder.mcuTv.setTextColor(Color.GREEN);
                } else {
                    holder.cphTv.setTextColor(Color.WHITE);
                    holder.imeiTv.setTextColor(Color.WHITE);
                    holder.mcuTv.setTextColor(Color.WHITE);
                }
                return view;
            }

        };
        mDeviceLv.setAdapter(mAdapter);
        mSound = new Sound(getContext());

    }

    private void setServer() throws Exception {
        AppConfig.mComManager.setADDRESS(Method.getServer().split(":")[0].trim());
        AppConfig.mComManager.setPOT(Integer.valueOf(Method.getServer().split(":")[1].trim()));
    }

    @Override
    public void onDestroyView() {
        PrefUtil.putInt(getContext(), PrefKey.COUNT, Method.getI());
        isExe = false;
        super.onDestroy();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    private void initState() {
        if (mEngineClick && isFirstInit) {
            mSound.play(Sound.NORMAL);
            isFirstInit = false;
        }
        mEngineCiv.setChecked(mEngineClick);
        MyUtil.setDrawableTop(getContext(), mFireTv, mEngineClick ? R.drawable.fire : R.drawable.misfire);
        MyUtil.setDrawableTop(getContext(), mMisFireTv, mEngineClick ? R.drawable.misfire : R.drawable.fire);
        mFindCarCiv.setChecked(mFindCarClick);
        mFrontFrostCiv.setChecked(mFrontFrostClick);
        mRearFrostCiv.setChecked(mRearFrostClick);
        mSearHeatCiv.setChecked(mSearHeatClick);
        mAirScv.setIsClick(mEngineClick);
        mAirScv.invalidate();
        mAirScv.setChangeSuccess(false);
    }

    @Override
    public void initData() {
        if (!Method.getTboxId().equals(PrefUtil.getString(getContext(), PrefKey.TBOX_ID, "")) && !Method.getTboxId().isEmpty()) {
            try {
                setServer();
            } catch (Exception e) {
                e.printStackTrace();
            }
            AppConfig.mComManager.loading(Method.getTboxId(), PrefUtil.getString(getContext(), PrefKey.LOGIN_PHONE, ""), PrefUtil.getString(getContext(), PrefKey.LOGIN_PASSWORD, ""));
            PrefUtil.putString(getContext(), PrefKey.TBOX_ID, Method.getTboxId());
        }
        AppConfig.mComManager.getCarInfo(PrefUtil.getString(getContext(), PrefKey.TBOX_ID, ""));
        AppConfig.mComManager.setOnRecvCommunicationListener(new CommunicationManager.OnRecvCommunicationListener() {
            @Override
            public void onRecv_B001(int answerID, byte[] message) {
                String describeStr = CommunicationManager.getCmdString(message, 18);
                if (describeStr.equals("设备不在线")) {
                    isNodevice = true;
                }
                handler.sendEmptyMessage(DEVICEONOROFF);
                if (describeStr.equals("没有登录")) {
                    handler.sendEmptyMessage(NOLOGIN);
                }
            }

            @Override
            public void onRecv_B601(String description) {

            }

            @Override
            public void onRecv_A602(List<String> llr) {

            }

            @Override
            public void onRecv_B500(byte[] message) {

            }

            @Override
            public void onRecv_B208(List<PhoneBandDevice> itemsDevices) {

            }

            @Override
            public void onRecv_CarStatus(byte[] carStatusBytes) {
                mCarStatusBytes = carStatusBytes;
                if (CommandExe) {
                    checkStateChange();
                }
                handler.sendEmptyMessage(CARSTATE);
//                Log.e("RWQ", Arrays.toString(mCarStatusBytes));


            }

            @Override
            public void onRecv_Gps(byte[] gpsBytes) {

            }

            @Override
            public void onConnectBreak() {

            }

            @Override
            public void onRecv_B208Error() {

            }
        });
    }

    private void checkStateChange() {
        switch (mCommandId) {
            case CommunicationManager.SEND_CONTROLL_CMD_FIRE_ON:
                if (CommunicationManager.G_BIT(mLastStateBytes, 0, 0, 1) == 0 && CommunicationManager.G_BIT(mCarStatusBytes, 0, 0, 1) == 1) {
                    mLastStateBytes = mCarStatusBytes;
                    handler.sendEmptyMessage(FIRE_ON);
                    ParamentInit();
                    break;
                } else {
                    CommandResponse();
                }
                break;
            case CommunicationManager.SEND_CONTROLL_CMD_FIRE_OFF:
                if (CommunicationManager.G_BIT(mLastStateBytes, 0, 0, 1) == 1 && CommunicationManager.G_BIT(mCarStatusBytes, 0, 0, 1) == 0) {
                    mLastStateBytes = mCarStatusBytes;
                    handler.sendEmptyMessage(FIRE_OFF);
                    ParamentInit();
                    break;
                } else {
                    CommandResponse();
                }
                break;
            case CommunicationManager.SEND_CONTROLL_CMD_FRONT_DEFROSTING_ON:
                if (CommunicationManager.G_BIT(mLastStateBytes, 3, 6, 1) == 0 && CommunicationManager.G_BIT(mCarStatusBytes, 3, 6, 1) == 1) {
                    handler.sendEmptyMessage(FRONT_DEFROSTING_ON);
                    ParamentInit();
                    break;
                } else {
                    CommandResponse();
                }
                break;
            case CommunicationManager.SEND_CONTROLL_CMD_FRONT_DEFROSTING_OFF:
                if (CommunicationManager.G_BIT(mLastStateBytes, 3, 6, 1) == 1 && CommunicationManager.G_BIT(mCarStatusBytes, 3, 6, 1) == 0) {
                    handler.sendEmptyMessage(FRONT_DEFROSTING_OFF);
                    ParamentInit();
                    break;
                } else {
                    CommandResponse();
                }
                break;
            case CommunicationManager.SEND_CONTROLL_CMD_REAR_DEFROSTING_ON:
                if (CommunicationManager.G_BIT(mLastStateBytes, 3, 7,
                        1) == 0 && CommunicationManager.G_BIT(mCarStatusBytes, 3, 7,
                        1) == 1) {
                    handler.sendEmptyMessage(REAR_DEFROSTING_ON);
                    ParamentInit();
                    break;
                } else {
                    CommandResponse();
                }
                break;
            case CommunicationManager.SEND_CONTROLL_CMD_REAR_DEFROSTING_OFF:
                if (CommunicationManager.G_BIT(mLastStateBytes, 3, 7,
                        1) == 1 && CommunicationManager.G_BIT(mCarStatusBytes, 3, 7,
                        1) == 0) {
                    handler.sendEmptyMessage(REAR_DEFROSTING_OFF);
                    ParamentInit();
                    break;
                } else {
                    CommandResponse();
                }
                break;
            case CommunicationManager.SEND_CONTROLL_CMD_SEAT_HEATING_ON:
                if (CommunicationManager.G_BIT(mLastStateBytes, 5, 0, 4) == 0 && CommunicationManager.G_BIT(mCarStatusBytes, 5, 0, 4) == 1) {
                    handler.sendEmptyMessage(SEAT_HEATING_ON);
                    ParamentInit();
                    break;
                } else {
                    CommandResponse();
                }
                break;
            case CommunicationManager.SEND_CONTROLL_CMD_SEAT_HEATING_OFF:
                if (CommunicationManager.G_BIT(mLastStateBytes, 5, 0, 4) == 1 && CommunicationManager.G_BIT(mCarStatusBytes, 5, 0, 4) == 0) {
                    handler.sendEmptyMessage(SEAT_HEATING_OFF);
                    ParamentInit();
                    break;
                } else {
                    CommandResponse();
                }
                break;
            case CommunicationManager.SEND_CONTROLL_CMD_COOL_WIND_L:
                if (CommunicationManager.G_BIT(mCarStatusBytes, 4, 1, 4) == 0 && CommunicationManager.G_BIT(mCarStatusBytes, 4, 5, 3) == 1) {
                    handler.sendEmptyMessage(COOL_WIND);
                    ParamentInit();
                    break;
                } else {
                    CommandResponse();
                }
                break;
            case CommunicationManager.SEND_CONTROLL_CMD_COOL_WIND_M:
                if (CommunicationManager.G_BIT(mCarStatusBytes, 4, 1, 4) == 0 && CommunicationManager.G_BIT(mCarStatusBytes, 4, 5, 3) == 3) {
                    handler.sendEmptyMessage(COOL_WIND);
                    ParamentInit();
                    break;
                } else {
                    CommandResponse();
                }
                break;
            case CommunicationManager.SEND_CONTROLL_CMD_COOL_WIND_H:
                if (CommunicationManager.G_BIT(mCarStatusBytes, 4, 1, 4) == 0 && CommunicationManager.G_BIT(mCarStatusBytes, 4, 5, 3) == 5) {
                    handler.sendEmptyMessage(COOL_WIND);
                    ParamentInit();
                    break;
                } else {
                    CommandResponse();
                }
                break;
            case CommunicationManager.SEND_CONTROLL_CMD_WARM_WIND_L:
                if (CommunicationManager.G_BIT(mCarStatusBytes, 4, 1, 4) == 10 && CommunicationManager.G_BIT(mCarStatusBytes, 4, 5, 3) == 1) {
                    handler.sendEmptyMessage(COOL_WIND);
                    ParamentInit();
                    break;
                } else {
                    CommandResponse();
                }
                break;
            case CommunicationManager.SEND_CONTROLL_CMD_WARM_WIND_M:
                if (CommunicationManager.G_BIT(mCarStatusBytes, 4, 1, 4) == 10 && CommunicationManager.G_BIT(mCarStatusBytes, 4, 5, 3) == 3) {
                    handler.sendEmptyMessage(COOL_WIND);
                    ParamentInit();
                    break;
                } else {
                    CommandResponse();
                }
                break;
            case CommunicationManager.SEND_CONTROLL_CMD_WARM_WIND_H:
                if (CommunicationManager.G_BIT(mCarStatusBytes, 4, 1, 4) == 10 && CommunicationManager.G_BIT(mCarStatusBytes, 4, 5, 3) == 5) {
                    handler.sendEmptyMessage(COOL_WIND);
                    ParamentInit();
                    break;
                } else {
                    CommandResponse();
                }
                break;
            case CommunicationManager.SEND_CONTROLL_CMD_AIR_OFF:
                if ((CommunicationManager.G_BIT(mLastStateBytes, 4, 1, 4) != 6 && CommunicationManager.G_BIT(mCarStatusBytes, 4, 1, 4) == 6) ||
                        (CommunicationManager.G_BIT(mLastStateBytes, 4, 5, 3) != 0 && CommunicationManager.G_BIT(mCarStatusBytes, 4, 5, 3) == 0)) {
                    handler.sendEmptyMessage(COOL_WIND);
                    ParamentInit();
                    break;
                } else {
                    CommandResponse();
                }
                break;
        }

    }

    private void ParamentInit() {
        firstTime = 0;
        CommandExe = false;
        count = 11;
    }


    private void CommandResponse() {
        Log.e("RWQ", count + "");
        handler.sendEmptyMessage(TIME);
        if (count > 10) {
            firstTime = 0;
            return;
        }
        if (count == 4) {
            AppConfig.mComManager.sendCtrlCar(mCommandId);
        }
        if (count == 10) {
            handler.sendEmptyMessage(TOAST);
        }
        if (count <= 10) {
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            if (AppConfig.mComManager != null) {
                AppConfig.mComManager.getCarInfo(PrefUtil.getString(getContext(), PrefKey.TBOX_ID, ""));
            }
        }

    }


    public void sendCommand(int cmdId) {
        mLastStateBytes = mCarStatusBytes;
        handler.sendEmptyMessageDelayed(MSG_SEND_CMD_TIMEOUT,20000);
        AppConfig.mComManager.sendCtrlCar(cmdId);
        AppConfig.mComManager.getCarInfo(PrefUtil.getString(getContext(), PrefKey.TBOX_ID, ""));
        mCommandId = cmdId;
        CommandExe = true;
        if (firstTime == 0) {
            count = 0;
            firstTime = SystemClock.currentThreadTimeMillis();
            mDialog.show();
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        isBandle();
        mTboxId = Method.getTboxId();
        if (AppConfig.user.getTboxinfo().size() == 1) {
            AppConfig.user.getTboxinfo().get(Method.getI()).setClicked(true);
            mAdapter.notifyDataSetChanged();
        }
        if (!isExe) {
            try {
                initTitleState();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        initData();
        addListener();
    }


    @Override
    public void addListener() {
        mEngineCiv.setOnClickListener(this);
        mDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
            @Override
            public void onCancel(DialogInterface dialogInterface) {
                count = 10;
                mDialog.dismiss();
                initDialog();
                addListener();
            }
        });
        mEngineCiv.setOnLongClickListener(new View.OnLongClickListener() {
            //            中控已锁
//            手刹拉起
//            档位为空挡
//            所有边门关闭
            @Override
            public boolean onLongClick(View view) {
                if (isNodevice) {
                    CommonUtil.showToast(getContext(), "设备不在线");
                    return true;
                }
                if (AppConfig.mComManager.isFalse) {
                    return true;
                }
                if (!mEngineClick) {
                    if (centerControl && handBrake && gearNull && allDoorClose) {
                        mEngineClick = !mEngineClick;

                        mAirScv.setIsClick(mEngineClick);
                        sendCommand(CommunicationManager.SEND_CONTROLL_CMD_FIRE_ON);
                    } else {
                        String string = "";
                        if (!centerControl) {
                            string = string + "中控未锁";
                        }
                        if (!handBrake) {
                            if (string.isEmpty()) {
                                string = string + "手刹未拉起";
                            } else {
                                string = string + ",手刹未拉起";
                            }
                        }
                        if (!gearNull) {
                            if (string.isEmpty()) {
                                string = string + "当前档位不是空挡";
                            } else {
                                string = string + ",当前档位不是空挡";
                            }
                        }
                        if (!allDoorClose) {
                            if (string.isEmpty()) {
                                string = string + "有车门未关闭";
                            } else {
                                string = string + ",有车门未关闭";
                            }
                        }
                        CommonUtil.showToast(getContext(), string + "，危险启动已取消启动指令!");
                    }
                }
                return true;
            }
        });
        mFindCarCiv.setOnClickListener(this);
        mFrontFrostCiv.setOnClickListener(this);
        mRearFrostCiv.setOnClickListener(this);
        mSearHeatCiv.setOnClickListener(this);
        mAirScv.setListener(new SevenClickView.onFlagChangeListener() {
            @Override
            public void onChange(int flag) {
                switch (flag) {
                    case -3:
                        sendCommand(CommunicationManager.SEND_CONTROLL_CMD_COOL_WIND_H);
                        break;
                    case -2:
                        sendCommand(CommunicationManager.SEND_CONTROLL_CMD_COOL_WIND_M);
                        break;
                    case -1:
                        sendCommand(CommunicationManager.SEND_CONTROLL_CMD_COOL_WIND_L);
                        break;
                    case 0:
                        sendCommand(CommunicationManager.SEND_CONTROLL_CMD_AIR_OFF);
                        break;
                    case 1:
                        sendCommand(CommunicationManager.SEND_CONTROLL_CMD_WARM_WIND_L);
                        break;
                    case 2:
                        sendCommand(CommunicationManager.SEND_CONTROLL_CMD_WARM_WIND_M);
                        break;
                    case 3:
                        sendCommand(CommunicationManager.SEND_CONTROLL_CMD_WARM_WIND_H);
                        break;
                }
            }
        });
    }

    @Override
    public void onPause() {
        super.onPause();
        isFirst = true;
        isGet = true;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mRootView = inflater.inflate(R.layout.fragment_control, null);
        initView();
        return mRootView;
    }

    @Override
    public void viewClick(View v) {
        switch (v.getId()) {
            case R.id.control_engine_civ:
                if (isNodevice) {
                    CommonUtil.showToast(getContext(), "设备不在线");
                    break;
                }
                if (AppConfig.mComManager.isFalse) {
                    break;
                }
                if (!mEngineClick) {
                    CommonUtil.showToast(getActivity(), "长按启动");
                } else {
                    mEngineClick = !mEngineClick;
                    mAirScv.setIsClick(mEngineClick);
                    sendCommand(CommunicationManager.SEND_CONTROLL_CMD_FIRE_OFF);
                }
                break;
            case R.id.control_findcar_civ:
                if (AppConfig.mComManager.isFalse) {
                    break;
                }
                mFindCarClick = !mFindCarClick;
                mFindCarCiv.setChecked(mFindCarClick);
                if (mFindCarClick) {
                    AppConfig.mComManager.sendCtrlCar(CommunicationManager.SEND_CONTROLL_CMD_SEARCHING_CAR);
                }
                handler.sendEmptyMessageDelayed(1, 2000);
                break;
            case R.id.control_frontdefrost_civ:
                if (AppConfig.mComManager.isFalse) {
                    break;
                }
                if (mEngineClick) {
                    if (mFrontFrostClick) {
                        sendCommand(CommunicationManager.SEND_CONTROLL_CMD_FRONT_DEFROSTING_OFF);
                    } else {
                        sendCommand(CommunicationManager.SEND_CONTROLL_CMD_FRONT_DEFROSTING_ON);
                    }
                    mFrontFrostClick = !mFrontFrostClick;
                } else {
                    CommonUtil.showToast(getContext(), R.string.carOpen);
                }
                break;
            case R.id.control_reardefrost_civ:
                if (AppConfig.mComManager.isFalse) {
                    break;
                }
                if (mEngineClick) {
                    if (mRearFrostClick) {
                        sendCommand(CommunicationManager.SEND_CONTROLL_CMD_REAR_DEFROSTING_OFF);
                    } else {
                        sendCommand(CommunicationManager.SEND_CONTROLL_CMD_REAR_DEFROSTING_ON);
                    }
                    mRearFrostClick = !mRearFrostClick;
                } else {
                    CommonUtil.showToast(getContext(), R.string.carOpen);
                }

                break;
            case R.id.control_seatheat_civ:
                if (AppConfig.mComManager.isFalse) {
                    break;
                }
                if (mEngineClick) {
                    if (mSearHeatClick) {
                        sendCommand(CommunicationManager.SEND_CONTROLL_CMD_SEAT_HEATING_OFF);
                    } else {
                        sendCommand(CommunicationManager.SEND_CONTROLL_CMD_SEAT_HEATING_ON);
                    }
                    mSearHeatClick = !mSearHeatClick;
                } else {
                    CommonUtil.showToast(getContext(), R.string.carOpen);
                }
                break;
            case R.id.common_titlebar_left_tv:
                if (AppConfig.user.getTboxinfo().size() == 0) {
                    CommonUtil.showToast(getContext(), "当前未绑定任何设备!");
                    CommonUtil.openActivity(getContext(), ConnectEquipmentActivity.class);
                } else {
                    showPopupWindow(v);
                }

                break;
            case R.id.common_titlebar_right_tv:
                CommonUtil.openActivity(getContext(), ConnectEquipmentActivity.class);
                break;
        }
    }

    private void showPopupWindow(View v) {
        popupWindow = new PopupWindow(contentView,
                ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);

        popupWindow.setTouchable(true);

        popupWindow.setTouchInterceptor(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {

                Log.i("mengdd", "onTouch : ");

                return false;
                // 这里如果返回true的话，touch事件将被拦截
                // 拦截后 PopupWindow的onTouchEvent不被调用，这样点击外部区域无法dismiss
            }
        });

        // 如果不设置PopupWindow的背景，无论是点击外部区域还是Back键都无法dismiss弹框
        // 我觉得这里是API的一个bug
        popupWindow.setBackgroundDrawable(getResources().getDrawable(
                R.drawable.bg_cph_tv));

        // 设置好参数之后再show
        popupWindow.showAsDropDown(v, 0, 20);
    }


    public void getCurrentStateSuccess() {
//        if (carState == null) {
//            if (isFirst) {
//                if (AppConfig.user.getTboxinfo().size() == 0) {
//
//                } else {
//                    showNoInfoDialog();
//                }
//                isFirst = false;
//            }
//
//            return;
//        }
//        if (isGet) {
//            CommonUtil.showToast(getContext(), "已获取到正确的数据!");
//            isGet = false;
//        }

        int acStatus = CommunicationManager.G_BIT(mCarStatusBytes, 4, 0, 1);
        // 调温档位
        int thermostatStatus = CommunicationManager.G_BIT(mCarStatusBytes, 4, 1,
                4);
        // 风机档位
        int fanStatus = CommunicationManager.G_BIT(mCarStatusBytes, 4, 5, 3);
        Log.e("rwq", "thermostatStatus: " + thermostatStatus + "  fanStatus:" + fanStatus);
        if (thermostatStatus == 6 || fanStatus == 0) {
            mAirScv.setFlag(0);
        } else if (thermostatStatus == 0) {// 冷
            switch (fanStatus) {
                case 1:
                    mAirScv.setFlag(-1);
                    break;
                case 3:
                    mAirScv.setFlag(-2);
                    break;
                case 5:
                    mAirScv.setFlag(-3);
                    break;
            }
        } else if (thermostatStatus == 10) {// 热
            switch (fanStatus) {
                case 1:
                    mAirScv.setFlag(1);
                    break;
                case 3:
                    mAirScv.setFlag(2);
                    break;
                case 5:
                    mAirScv.setFlag(3);
                    break;
            }
        }
        int sim = CommunicationManager.G_BYTE(mCarStatusBytes, 6);
//        //信号状态
        if (sim == 0) {
            mSimTv.setCompoundDrawablesWithIntrinsicBounds(R.mipmap.signal0, 0, 0, 0);
            mSimTv.setText("没有信号");
        } else if (sim > 0 && sim <= 8) {
            mSimTv.setCompoundDrawablesWithIntrinsicBounds(R.mipmap.signal1, 0, 0, 0);
            mSimTv.setText("  信号弱");
        } else if (sim > 8 && sim <= 16) {
            mSimTv.setCompoundDrawablesWithIntrinsicBounds(R.mipmap.signal2, 0, 0, 0);
            mSimTv.setText("信号一般");
        } else if (sim > 16 && sim <= 24) {
            mSimTv.setCompoundDrawablesWithIntrinsicBounds(R.mipmap.signal3, 0, 0, 0);
            mSimTv.setText("信号中等");
        } else if (sim > 24 && sim <= 32) {
            mSimTv.setCompoundDrawablesWithIntrinsicBounds(R.mipmap.signal_normal, 0, 0, 0);
            mSimTv.setText("信号正常");
        }
        //电量状态
        // 剩余能源 %
        int residualEnergyStatus = CommunicationManager.G_BYTE(mCarStatusBytes,
                7);
        if (residualEnergyStatus > 10) {//10-25
            mPetrol.setCompoundDrawablesWithIntrinsicBounds(R.mipmap.petrol_normal, 0, 0, 0);
            mPetrol.setText("油量：" + residualEnergyStatus + "%" + "    ");
        } else {//0-10
            mPetrol.setCompoundDrawablesWithIntrinsicBounds(R.mipmap.petrol_low, 0, 0, 0);
            mPetrol.setText("油量：" + residualEnergyStatus + "%" + "    ");
        }

        // 灯光状态
        int lamplightStatus = CommunicationManager.G_BIT(mCarStatusBytes, 3, 0,
                4);
        switch (lamplightStatus) {
            case 0:
                mLightTv.setCompoundDrawablesWithIntrinsicBounds(R.mipmap.control_light_close, 0, 0, 0);
                mLightTv.setText("灯光已关");
                break;
            case 1:
                mLightTv.setCompoundDrawablesWithIntrinsicBounds(R.mipmap.control_light_open, 0, 0, 0);
                mLightTv.setText("灯光已开");
                break;
            default:
                mLightTv.setCompoundDrawablesWithIntrinsicBounds(R.mipmap.control_light_close, 0, 0, 0);
                mLightTv.setText("灯光已关");
                break;
        }
        //        左前右前 左后右后  车门状态
        //        0 00000 00
        // 中控门锁
        int doorLockStatus = CommunicationManager
                .G_BIT(mCarStatusBytes, 1, 0, 1);
        switch (doorLockStatus) {
            case 1:
                mCenterControlTv.setCompoundDrawablesWithIntrinsicBounds(R.mipmap.center_control_lock, 0, 0, 0);
                mCenterControlTv.setText("中控已锁");
                centerControl = true;
                break;
            case 0:
                mCenterControlTv.setCompoundDrawablesWithIntrinsicBounds(R.mipmap.center_control_unlock, 0, 0, 0);
                mCenterControlTv.setText("中控未锁");
                centerControl = false;
                break;
        }
        // 边门状态
        int doorStatus = CommunicationManager.G_BIT(mCarStatusBytes, 1, 1, 1);

        int doorStatus_fl = CommunicationManager.G_BIT(mCarStatusBytes, 1, 2, 1);//前左
        int doorStatus_fr = CommunicationManager.G_BIT(mCarStatusBytes, 1, 3, 1);//前右
        int doorStatus_bl = CommunicationManager.G_BIT(mCarStatusBytes, 1, 4, 1);//后左
        int doorStatus_br = CommunicationManager.G_BIT(mCarStatusBytes, 1, 5, 1);//后右
        int doorStatus_f = CommunicationManager.G_BIT(mCarStatusBytes, 1, 6, 1);//尾门
        int doorStatus_l = CommunicationManager.G_BIT(mCarStatusBytes, 1, 7, 1);//前引擎盖
        if (doorStatus == 1) {
            if ((doorStatus_fr == 1 || doorStatus_br == 1) && doorStatus_f == 1) {//右开后开
                mDoorTv.setCompoundDrawablesWithIntrinsicBounds(R.mipmap.door_right_tail, 0, 0, 0);
                mDoorTv.setText("右、后边门未关");
            }
            if ((doorStatus_fr == 1 || doorStatus_br == 1) && doorStatus_f == 0) {//右开后关
                mDoorTv.setCompoundDrawablesWithIntrinsicBounds(R.mipmap.door_right, 0, 0, 0);
                mDoorTv.setText("右边门未关");
            }
            if ((doorStatus_fl == 1 || doorStatus_bl == 1) && doorStatus_f == 1) {//左开后开
                mDoorTv.setText("左、后边门未关");
                mDoorTv.setCompoundDrawablesWithIntrinsicBounds(R.mipmap.door_left_tail, 0, 0, 0);
            }
            if ((doorStatus_fl == 1 || doorStatus_bl == 1) && doorStatus_f == 0) {//左开后开
                mDoorTv.setText("左边门未关");
                mDoorTv.setCompoundDrawablesWithIntrinsicBounds(R.mipmap.door_left, 0, 0, 0);
            }
            if (doorStatus_f == 0 && (doorStatus_fl == 1 || doorStatus_bl == 1) && (doorStatus_fr == 1 || doorStatus_br == 1)) {
                mDoorTv.setText("左右后边门未关");
                mDoorTv.setCompoundDrawablesWithIntrinsicBounds(R.mipmap.door_all_open, 0, 0, 0);
            }
            if (doorStatus_f == 1 && (doorStatus_fl == 1 || doorStatus_bl == 1) && (doorStatus_fr == 1 || doorStatus_br == 1)) {
                mDoorTv.setCompoundDrawablesWithIntrinsicBounds(R.mipmap.door_right_left, 0, 0, 0);
            }
            if ((doorStatus_fl == 0 && doorStatus_bl == 0) && (doorStatus_fr == 0 && doorStatus_br == 0) && (doorStatus_f == 1)) {
                mDoorTv.setText("后边门未关");
                mDoorTv.setCompoundDrawablesWithIntrinsicBounds(R.mipmap.door_tail, 0, 0, 0);
            }
        } else if (doorStatus == 0) {
            //            所有车门关闭
            mDoorTv.setCompoundDrawablesWithIntrinsicBounds(R.mipmap.door_all_close, 0, 0, 0);
            mDoorTv.setText("边门关闭    ");
            allDoorClose = true;

        }
        //档位设置
        int stallStatus = CommunicationManager.G_BIT(mCarStatusBytes, 0, 4, 4);
        switch (stallStatus) {
            case 0:
                mGearTv.setCompoundDrawablesWithIntrinsicBounds(R.mipmap.control_gear_n, 0, 0, 0);
                gearNull = true;
                break;
            case 1:
                gearNull = false;
                mGearTv.setCompoundDrawablesWithIntrinsicBounds(R.mipmap.control_gear_d, 0, 0, 0);
                break;
            case 3:
                gearNull = false;
                mGearTv.setCompoundDrawablesWithIntrinsicBounds(R.mipmap.control_gear_r, 0, 0, 0);
                break;
        }
        //手刹状态
        int handbrakeStatus = CommunicationManager.G_BIT(mCarStatusBytes, 0, 1, 1);
        switch (handbrakeStatus) {
            case 1:
                mHandBrakeTv.setCompoundDrawablesWithIntrinsicBounds(R.mipmap.control_handbake_up, 0, 0, 0);
                mHandBrakeTv.setText("手刹拉起");
                handBrake = true;
                break;
            case 0:
                mHandBrakeTv.setCompoundDrawablesWithIntrinsicBounds(R.mipmap.control_handbrake_down, 0, 0, 0);
                mHandBrakeTv.setText("手刹松开");
                handBrake = false;
                break;
            default:
                mHandBrakeTv.setCompoundDrawablesWithIntrinsicBounds(R.mipmap.control_handbrake_down, 0, 0, 0);
                mHandBrakeTv.setText("手刹松开");
                handBrake = false;
                break;
        }

        //引擎状态
        switch (CommunicationManager.G_BIT(mCarStatusBytes, 0, 0, 1)) {
            case 0:
                mEngineClick = false;
                break;
            case 1:
                mEngineClick = true;
                break;
        }
        // 前除霜状态
        int frontDefrostingStatus = CommunicationManager.G_BIT(mCarStatusBytes, 3,
                6, 1);
        switch (frontDefrostingStatus) {
            case 0:
                mFrontFrostClick = false;
                break;
            case 1:
                mFrontFrostClick = true;
                break;
        }
        // 后除霜状态
        int rearDefrostingStatus = CommunicationManager.G_BIT(mCarStatusBytes, 3, 7,
                1);
        switch (rearDefrostingStatus) {
            case 0:
                mRearFrostClick = false;
                break;
            case 1:
                mRearFrostClick = true;
                break;
        }
        // 座椅加热状态
        int seatHeatingStatus = CommunicationManager.G_BIT(mCarStatusBytes, 5, 0, 4);
        switch (seatHeatingStatus) {
            case 0:
                mSearHeatClick = false;
                break;
            default:
                mSearHeatClick = true;
                break;
        }
        initState();
    }


    public void isBandle() {
        if (AppConfig.user.getTboxinfo().size() == 0) {
            showNotBindDialog();
        }
    }

    private void showNotBindDialog() {
        CustomDialog.Builder builder = new CustomDialog.Builder(getContext());
        builder.setTitle(R.string.hint);
        builder.setMessage("您还没有绑定设备,请先绑定设备再使用");
        builder.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                goToBindUser();
                dialog.dismiss();
            }
        });
        builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                mTabHost.setCurrentTab(0);
                dialog.dismiss();
            }
        });
        CustomDialog dialog = builder.create();
        dialog.setCancelable(false);
        dialog.show();
    }

    private void showNoInfoDialog() {
        CustomDialog.Builder builder = new CustomDialog.Builder(getContext());
        builder.setTitle(R.string.hint);
        builder.setMessage("设备不在线，请到设备列表切换设备");
        builder.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                mTabHost.setCurrentTab(0);
                dialog.dismiss();
            }
        });
        builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                CommonUtil.showToast(getContext(), "请切换设备");
                dialog.dismiss();
            }
        });
        builder.create().show();
    }

    /**
     * 打开绑定设备页
     */
    private void goToBindUser() {
        CommonUtil.openActivity(getContext(), ConnectEquipmentActivity.class);
    }


}
